package com.dingteam.question.common.validation.module.text;

import com.dingteam.model.Result;
import com.dingteam.question.common.dto.QuestionPaperVerifyDTO;
import com.dingteam.question.common.dto.QuestionResultVerifyDTO;
import com.dingteam.question.common.enums.ModuleEnum;
import com.dingteam.question.common.validation.module.AbstractModuleTextValidator;
import com.dingteam.validation.util.RegexUtils;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @author Jamin
 * @date 2019/08/14
 */
@Component
public class EmailValidator extends AbstractModuleTextValidator {

	@Getter
	private final ModuleEnum module = ModuleEnum.EMAIL;

	@Override
	public boolean supportModule(ModuleEnum moduleEnum) {
		return module.equals(moduleEnum);
	}

	@Override
	public Result<String> createValid(QuestionPaperVerifyDTO.QuestionItemDTO itemDTO) {
		return validCreateCommon(itemDTO);
	}

	@Override
	public Result<String> submitValid(QuestionResultVerifyDTO.ItemResultDTO item) {
		Result<String> result = validSubmitCommon(item);
		if (!result.getSuccess()) {
			return result;
		}
		if (StringUtils.isNotBlank(item.getContent())) {
			if (!RegexUtils.isEmail(item.getContent())) {
				return Result.error("{validation.text.telephone.illegal.message}", EMPTY);
			}
		}
		return result;
	}
}
